package cn.ghf.order.service.impl;


import cn.ghf.basic.constant.PayBillBusinessType;
import cn.ghf.basic.service.impl.BaseServiceImpl;
import cn.ghf.basic.util.CodeGenerateUtils;
import cn.ghf.order.domain.AdoptOrder;
import cn.ghf.order.domain.OrderAddress;
import cn.ghf.order.domain.ProductOrder;
import cn.ghf.order.mapper.OrderAddressMapper;
import cn.ghf.order.mapper.ProductOrderMapper;
import cn.ghf.order.service.IProductOrderService;
import cn.ghf.pay.domain.PayBill;
import cn.ghf.pay.mapper.PayBillMapper;
import cn.ghf.pay.service.IPayBillService;
import cn.ghf.product.domain.Product;
import cn.ghf.product.mapper.ProductMapper;
import cn.ghf.user.domain.LoginInfo;
import cn.ghf.user.domain.User;
import cn.ghf.user.domain.UserAddress;
import cn.ghf.user.mapper.UserAddressMapper;
import cn.ghf.user.mapper.UserMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Map;

@Service
public class ProductOrderServiceImpl extends BaseServiceImpl<ProductOrder> implements IProductOrderService {

    @Autowired
    private ProductMapper productMapper;

    @Autowired
    private ProductOrderMapper productOrderMapper;


    @Autowired
    private UserMapper userMapper;

    @Autowired
    private UserAddressMapper userAddressMapper;

    @Autowired
    private OrderAddressMapper orderAddressMapper;

    @Autowired
    private PayBillMapper payBillMapper;

    @Autowired
    private IPayBillService payBillService;

    @Override
    public String submit(Map<String, Object> params, LoginInfo loginInfo) {
        //获取参数
        Long productId = Long.valueOf(params.get("product_id").toString()); //服务id
        Long addressId = Long.valueOf(params.get("address_id").toString());//用户地址id
        Integer serviceMethod = Integer.valueOf(params.get("service_method").toString()); //配送方式
        Integer payMethod = Integer.valueOf(params.get("pay_method").toString()); //支付方式
        Integer num = Integer.valueOf(params.get("num").toString()); //购买数量

        //1.增加购买数量
        Product product = productMapper.loadById(productId);
        Long salecount = product.getSalecount();
        product.setSalecount(salecount+num);

        //4.更新product
        productMapper.update(product);

        // 生成订单
        //获取productOrder
        User user = userMapper.loadByLoginInfoId(loginInfo.getId());
        ProductOrder productOrder = initProductOrder(user, product);
        //生成统一支付单号
        String unionPaySn = CodeGenerateUtils.generateUnionPaySn();
        productOrder.setPaySn(unionPaySn);
        productOrderMapper.add(productOrder);
        //获取orderAddress ，根据用户地址id查询用户地址信息
        UserAddress userAddress = userAddressMapper.loadById(addressId);
        OrderAddress orderAddress = userAddress2OrderAddress(userAddress, productOrder);
        //保存
        orderAddressMapper.add(orderAddress);

        //创建支付订单并发起支付请求
        //创建支付订单
        PayBill payBill = buildPayBill(payMethod, user, productOrder);
        //保存
        payBillMapper.add(payBill);
        //发起支付请求
        String payData = payBillService.payRequest(payBill);
        return payData;
    }

    /**
     * 创建支付订单
     * @param payMethod
     * @param user
     * @param adoptOrder
     * @return
     */
    private PayBill buildPayBill(Integer payMethod,User user, ProductOrder productOrder) {
        PayBill payBill = new PayBill();
        payBill.setDigest(productOrder.getDigest());
        payBill.setMoney(productOrder.getPrice());
        payBill.setUnionPaySn(productOrder.getPaySn());
        payBill.setState(0);
        payBill.setLastPayTime(new Date(System.currentTimeMillis()+15*50*1000));
        payBill.setPayChannel(Long.valueOf(payMethod));//0 余额 1 支付宝 2 微信 3 银联
        payBill.setBusinessType(PayBillBusinessType.BUSINESS_TYPE_PRODUCT);
        payBill.setBusinessKey(productOrder.getId());
        payBill.setUser_id(user.getId());
        payBill.setShop_id(productOrder.getShop_id());
        payBill.setNickName(user.getUsername());
        return payBill;
    }

    private OrderAddress userAddress2OrderAddress(UserAddress userAddress, ProductOrder productOrder) {
        OrderAddress orderAddress = new OrderAddress();
        BeanUtils.copyProperties(userAddress,orderAddress);
        orderAddress.setId(null);
        orderAddress.setOrder_id(productOrder.getId());
        orderAddress.setOrderSn(productOrder.getOrderSn());
        return orderAddress;
    }

    private ProductOrder initProductOrder(User user, Product product) {
        ProductOrder productOrder = new ProductOrder();
        productOrder.setDigest("[摘要]领养"+product.getName()+"的订单！");
        productOrder.setState(0); //待支付
        productOrder.setPrice(product.getSaleprice()); //待支付
        productOrder.setOrderSn(CodeGenerateUtils.generateOrderSn(user.getId()));
        //lastPayTime最后支付时间  当前时间+15分钟
        Date lastPayTime = new Date(System.currentTimeMillis()+15*60*1000);
        productOrder.setLastPayTime(lastPayTime);
        productOrder.setProduct_id(product.getId());
        productOrder.setUser_id(user.getId());
        productOrder.setShop_id(product.getShop_id());
        return productOrder;
    }
}
